﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace libManagedMessageSystem
{
    unsafe class libMessageSystemApi
    {
        public delegate void OnConnectSuccess(IntPtr mc,IntPtr ud);
        public delegate void OnConnectFailed(Int32 ec, IntPtr ud);
        public delegate void OnAccept(IntPtr mc, IntPtr ud);
        public delegate void OnLost(IntPtr mc);
        public delegate void OnMsg(IntPtr mc, byte* pBase, UInt32 length, UInt16 nDispatchId, UInt16 nMessageId);

        [DllImport("libMessageSystem.dll",CharSet=CharSet.Auto)]
        public static extern IntPtr MessageSystemCreate();

        [DllImport("libMessageSystem.dll", CharSet = CharSet.Auto)]
        public static extern void MessageSystemDestroy(IntPtr ms);

        [DllImport("libMessageSystem.dll", CharSet = CharSet.Auto)]
        public static extern void MessageSystemPoll(IntPtr ms, UInt32 wait);

        [DllImport("libMessageSystem.dll", CharSet = CharSet.Auto)]
        public static extern void MessageSystemSetOption(IntPtr ms, UInt32 key, UInt32 value);

        [DllImport("libMessageSystem.dll", CharSet = CharSet.Ansi)]
        public static extern void MessageSystemConnectRemoteSystem(IntPtr ms,String ip,UInt16 port,OnConnectSuccess s,OnConnectFailed f,IntPtr ud);

        [DllImport("libMessageSystem.dll", CharSet = CharSet.Ansi)]
        public static extern void MessageSystemListenLocalSystem(IntPtr ms, String ip, UInt16 port, OnAccept a, IntPtr ud);

        [DllImport("libMessageSystem.dll", CharSet = CharSet.Ansi)]
        public static extern void MessageChannelOpen(IntPtr mc, OnLost l, OnMsg m);

        [DllImport("libMessageSystem.dll", CharSet = CharSet.Ansi)]
        public static extern void MessageChannelClose(IntPtr mc);

        [DllImport("libMessageSystem.dll", CharSet = CharSet.Ansi)]
        public static extern bool MessageChannelSend(IntPtr mc, byte* pData, UInt32 nLength, UInt16 nMessageId, UInt16 nDispatchId);

        [DllImport("libMessageSystem.dll", CharSet = CharSet.Ansi)]
        public static extern bool MessageChannelBroadCast(IntPtr mc, byte* pData, UInt32 nLength, UInt16 nMessageId, UInt16[] nDispatchIds, UInt16 nDispatchCount);
    }
}
